var allHalfStar = $("#comment-form .score-star .half-star");//所有的半星
var score = 0;//初始分数
var textareaContent;//用来存放初始化评论值
//用户登录成功后显示初始数据
var initScore = $("#comment-form .score-msg-container .score span.num").html();

$(function(){
	textareaContent = $("#comment-form .content-container textarea").val(); 
	if(initScore != ""){
		score = parseInt(initScore);
		changeText(score);
		showTag(1);
		setStar(score);
	}else{
		showTag(0);
		setStar(score);
	}
	checkText();
});


//设置点亮星星的个数
function setStar(value){
    for (var i = 0; i < value; i++) {
        allHalfStar.eq(i).addClass("active");
    }
    for (var i = 10; i >= value; i--) {
        allHalfStar.eq(i).removeClass("active");
    }
}

//绑定事件
for (var i = 0; i < allHalfStar.length; i++) {
    //鼠标移入的时候点亮对应的星星
    $(allHalfStar).eq(i).mouseover(function(){
        var index = $(allHalfStar).index(this);
        setStar(index);
        $(this).addClass("active");
        changeText(parseInt(index+1));
        showTag(index+1);
    });

    //鼠标移出的时候将星星恢复成之前得分
    $(allHalfStar).eq(i).mouseleave(function(){
        setStar(score);
        changeText(parseInt(score));
        showTag(score);
    });

    //鼠标点击的时候将更新得分
    $(allHalfStar).eq(i).click(function(){
        var index = $(allHalfStar).index(this);
        score = index+1;
    });
}

//改变文本内容
function changeText(value){
    $("#comment-form .score-msg-container .score .num").eq(0).html(value);
    $("input[name='score']").val(parseInt(score));
    var text = $("#comment-form .score-msg-container .score-message");
    if(value == 1 || value == 2){
        text.html("超烂啊");
    }else if(value == 3 || value == 4){
        text.html("比较差");
    }else if(value == 5 || value == 6){
        text.html("一般般");
    }else if(value == 7 || value == 8){
        text.html("比较好");
    }else if(value == 9 || value == 10){
        text.html("棒极了");
    }
}

//当未评分是不显示得分标签
function showTag(value){
    if(value == 0){
        $("#comment-form .score-msg-container .score").css("display","none");
        $("#comment-form .score-msg-container .score-message").css("display","none");
        $("#comment-form .score-msg-container .no-score").attr("style","display:block");
    }else{
        $("#comment-form .score-msg-container .score").attr("style","display:block");
        $("#comment-form .score-msg-container .score-message").attr("style","display:block");
        $("#comment-form .score-msg-container .no-score").attr("style","display:none");
    }
}

//检测评论字数
function checkText(){
    var len = $("#comment-form .content-container textarea").val().length;
    if(len < 6){
        $(".content-container .word-count-info").attr("style","color:#999");
        $(".content-container .word-count-info").html("还需输入"+(Math.abs(len-6))+"个字");
        $("#subReview").attr("class","btn disabled");
        $("#subReview").attr("disabled",true);
    }
    if(len >= 6){
        $(".content-container .word-count-info").html("");
        $("#subReview").removeClass("disabled");
        $("#subReview").attr("disabled",false);
    }
    if(len > 279){
        $(".content-container .word-count-info").attr("style","color:#999");
        $(".content-container .word-count-info").html("还能输入"+(Math.abs(len-300))+"个字");
    }
    if(len > 300){
        $(".content-container .word-count-info").attr("style","color:red");
        $(".content-container .word-count-info").html("超出了"+(Math.abs(300-len))+"个字");
        $("#subReview").attr("class","btn disabled");
        $("#subReview").attr("disabled",true);
    }
    if(len == 300){
        $(".content-container .word-count-info").html("");
    }
}

$("#textarea").keyup(checkText);

$("#comment-form-container .close").click(function(){
    $("#comment-form-container").hide(300);
});

$(".action-buyBtn .action a").click(function(){
	//重置评论内容
	$("#comment-form .content-container textarea").val(textareaContent);
	$("#comment-form-container").show(300);
	checkText();
})

$(".comment-entry").click(function(){
	//重置评论内容
	$("#comment-form .content-container textarea").val(textareaContent);
	$("#comment-form-container").show(300);
	checkText();
})

var flag = $("#comment-form .content-container textarea").attr("flag");
$("#comment-form .content-container textarea").val(flag);

//设置点亮星星的个数,点亮所有用户评论的星星
 function stars(value,obj){
	 var allHalfStars = $(obj);
     for (var i = 0; i < parseInt(value); i++) {
         allHalfStars.find("i").eq(i).addClass("current");
     }
     showTag(value);
 }
 
 //给点赞绑定点击事件
 $(".comment-container .approve .approve-icon").click(function(){
	 var num1 = $(this).parents(".approve").attr("uId");
	 var num = $(this).parents(".approve").find(".num").html();
	 if(num1 != 'haha'){
		var revId =  $(this).parents(".approve").attr("revId");
		if($(this).attr("class").indexOf("active") != -1){
			$(this).parents(".approve").find(".num").html(parseInt(num)-1);
			$(this).removeClass("active");	
			
			var uId = $(this).parents(".approve").attr("uId");
			var revId = $(this).parents(".approve").attr("revId");
			var praise = $(this).parents(".approve").find(".num").html();
			
			$.ajax({
				url:"clearReviewPriase.do",
				type:"post",
				data:{
					"uId":uId,
					"revId":revId,
					"praise":praise
				},
				dataType:"json",
				success:function(data){
				}
			})
			
		}else{
			$(this).parents(".approve").find(".num").html(parseInt(num)+1);
			$(this).addClass("active");
			
			var uId = $(this).parents(".approve").attr("uId");
			var revId = $(this).parents(".approve").attr("revId");
			var praise = $(this).parents(".approve").find(".num").html();
			$.ajax({
				url:"reviewPriase.do",
				type:"post",
				data:{
					"uId":uId,
					"revId":revId,
					"praise":praise
						
				},
				dataType:"json",
				success:function(data){
				}
			})
		}
		
		 
	 }else{
		 location.href = "login.jsp";
	 }
	

 })
